const router = require('koa-router')()
const {postRole, getRole, getRoleById, deleteRole, patchRole, getRoleAll} = require('../../controller/role')

router.prefix('/api/role')

router.get('/role/all', async (ctx, next) => {
  ctx.body = await getRoleAll()
})

router.get('/role/:id', async (ctx, next) => {
  const {id} = ctx.params
  ctx.body = await getRoleById(id)
})

router.get('/role', async (ctx, next) => {
  const {name, isSchool, description, pageIndex, pageSize} = ctx.query
  const createTime = ctx.query['createTime[]']
  ctx.body = await getRole({name, isSchool, description, pageIndex, pageSize, createTime})
})

router.post('/role', async (ctx, next) => {
  const {name, auth, isSchool, description} = ctx.request.body
  ctx.body = await postRole({name, auth, isSchool, description})
})

router.patch('/role', async (ctx, next) => {
  const {name, isSchool, auth, description, id} = ctx.request.body
  ctx.body = await patchRole({name, isSchool, auth, description, id})
})

router.delete('/role/:id', async (ctx, next) => {
  const {id} = ctx.params
  ctx.body = await deleteRole(id)
})

module.exports = router
